package MyLinkedList;

import java.util.List;
import java.util.ListIterator;

public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;


        public ListNode (int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    public boolean contains (int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst (int val) {
        ListNode newH = new ListNode(val);
        if (head == null) {
            head = last = newH;
        } else {
            newH.next = head;
            head.prev = newH;
            head = newH;
        }
    }

    public void addLast(int val) {
        ListNode newH = new ListNode(val);
        if(head == null) {
            head = last = newH;
        } else {
            newH.prev = last;
            last.next = newH;
            last = newH;
        }
    }

    public void addIndex(int pos , int val) {
        if (pos > size() || pos < 0) {
            System.out.println("不合法！");
        }
        if (pos == 0) {
            addFirst(val);
            return;
        }
        if (pos == size()) {
            addLast(val);
            return;
        }
        ListNode node = new ListNode(val);
        ListNode cur = head;
        while (pos != 0) {
            cur = cur.next;
            pos--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }

    public void remove(int key) {
        if (head.val == key) {
            if(head.next != null) {
                head = head.next;
                return;
            } else {
                head = null;
            }
        } else if (last.val == key) {
            last = last.prev;
            last.next = null;
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                break;
            }
            cur = cur.next;
        }
    }

    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                if (cur == head) {
                    head = cur.next;
                } else if (cur == last) {
                    cur.prev.next = null;
                    last = cur.prev;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }

            }
            cur = cur.next;
        }
    }
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        head = null;
        last = null;
    }
}
